package com.hro.hro_app.gcm;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import android.util.Log;

import com.hro.hro_app.dataController.TeacherController;
import com.hro.hro_app.model.Device;
import com.hro.hro_app.model.Teacher;
import com.hro.hro_app.util.Properties;

public class ServerUtil {

	public static void register(Device device) {

		Log.i(Properties.TAG, "Register device on application server... id: " + device.getDeviceId());

		// Ophalen teachers
		device.addTeacherList(TeacherController.getTeacherList());

		// Post het device en bijbehorende teachers op de server.
		try {

			URL url = new URL(Properties.server + "/RegistrationServlet");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
			conn.setConnectTimeout(Properties.conTimeout);
			// post the request
			OutputStream out = conn.getOutputStream();

			// serialiseer het object naar de server.
			Serializer serializer = new Persister();
			serializer.write(device, out);

			// handle the response
			int status = conn.getResponseCode();
			if (status != 200) {
				throw new IOException("Post failed with error code " + status);
			}

			out.close();
		}
		catch (Exception e) {
			Log.e(Properties.TAG, e.getMessage());
		}

		// Vraag de huidige absente teachers op.
		try {

			URL url = new URL(Properties.server + "/TeacherStateServlet?id=" + device.getDeviceId());
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			InputStream in = conn.getInputStream();

			Serializer serializer = new Persister();
			device = serializer.read(Device.class, in);

			ArrayList<Teacher> absent = new ArrayList<Teacher>();
			for (int i = 0; i < device.getTeacherCount(); i++) {
				absent.add(device.getTeacherAt(i));
			}

			TeacherController.updateTeacherPresence(absent);

			in.close();
		}
		catch (Exception e) {
			Log.e(Properties.TAG, e.getMessage());
		}
	}

	public static void unregister(Device device) {

		Log.i(Properties.TAG, "Unregister device on application server... id: " + device.getDeviceId());

		try {

			URL url = new URL(Properties.server + "/UnRegistrationServlet");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
			conn.setConnectTimeout(Properties.conTimeout);
			// post the request
			OutputStream out = conn.getOutputStream();

			// serialiseer het object naar de server.
			Serializer serializer = new Persister();
			serializer.write(device, out);

			// handle the response
			int status = conn.getResponseCode();
			if (status != 200) {
				throw new IOException("Post failed with error code " + status);
			}

			out.close();
		}
		catch (Exception e) {
			Log.e(Properties.TAG, e.getMessage());
		}
	}
}
